<?php
    Yii::import('Cms.models.Contact');
    class ContactFormWidget extends FWidget
    {
        public function run()
        {
            $model=new Contact('search');
            //Perform an ajax validation 
            $this->performAjaxValidation($model);
            if(isset($_POST['Contact']))
            {
                $model=$this->controller->postModel('Contact');
                //var_dump($model);
                //echo $model->hasErrors();
                //$model->save();
                //$model->validate();
                if(!$model->save())
                {
                    var_dump($model->getErrors());
                }
                else echo "save success";
                //$model->getErrors();
            }
            else
            {
                //Do something
            }
            //Register script
            //$this->registerScript();
            return $this->render('ContactForm', array('model'=>$model), true);
            
        }
        
        /**
         * Performs the AJAX validation.
         * @param CModel the model to be validated
         */
        protected function performAjaxValidation($model)
        {
            if(isset($_POST['ajax']) && $_POST['ajax']==='contact-form')
            {
                echo CActiveForm::validate($model);
                Yii::app()->end();
            }
        }
        
        protected function registerScript()
        {
            //$baseScriptUrl=Yii::app()->getAssetManager()->publish(dirname(__FILE__).DIRECTORY_SEPARATOR.'assets');
            //$themeUrl = Yii::app()->request->hostInfo.Yii::app()->baseUrl.'/themes/'.Yii::app()->cmsManager->CmsTheme->name;
            //$cs = Yii::app()->clientScript;
            //$cs->registerCoreScript('jquery.ui');
            //$cs->registerScriptFile($themeUrl.'/scripts/ui.checkbox.js');
            //$cs->registerScriptFile($baseScriptUrl.'/jquery.meio.mask.js';
            //$cs->registerCssFile($themeUrl.'/styles/checkbox.css');
            //$cs->registerCssFile($themeUrl.'/styles/form.css');
            
        }
    }
?>
